#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
@Time        : 2021/11/11 14:53
@Author      : Albert Darren
@Contact     : 2563491540@qq.com
@File        : high_low_filter.py
@Version     : Version 1.0.0
@Description : TODO
@Created By  : PyCharm
"""
from numpy import fft, abs, zeros_like
from skimage.io import imread
from DIP_experiment_5.util import contrast_show
import copy

PATH = "../experiment_fig/brain.tif"
font_path = "C:/Windows/Fonts/simhei.ttf"
im = imread(PATH)
# fft2傅里叶变换
freq_im = fft.fft2(im)  # complex128
# fftshift幅度谱平移
magnitude_spectra_shift = fft.fftshift(freq_im)
copy_shift = copy.deepcopy(magnitude_spectra_shift)
# high-pass filter
rows, cols = im.shape
rows_num, cols_num = rows // 2, cols // 2
magnitude_spectra_shift[rows_num - 16:rows_num + 16, cols_num - 16:cols_num + 16] = 0
# low-pass filter
binary_template = zeros_like(im)
binary_template[rows_num - 16:rows_num + 16, cols_num - 16:cols_num + 16] = 1
low_pass_shift = copy_shift * binary_template
# ifftshift
high_magnitude_spectra_shift = fft.ifftshift(magnitude_spectra_shift)
low_magnitude_spectra_shift = fft.ifftshift(low_pass_shift)
# ifft2
high_iim = fft.ifft2(high_magnitude_spectra_shift)
low_iim = fft.ifft2(low_magnitude_spectra_shift)
# 幅度谱
high_iim = abs(high_iim)
low_iim = abs(low_iim)
im_dict = {"原始图像": im, "高通滤波": high_iim, "低通滤波": low_iim}
contrast_show(im_dict, (1, 3), font=font_path)
